package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.POSLoyaltyCard;

public class POSLoyaltyCardPresenter extends AbstractPresenter<POSLoyaltyCard> {
	final String SINGLEITEM = "posLoyaltyCard";
	final String MULTIITEM = "posLoyaltyCards";
	
	public ArrayList<POSLoyaltyCard> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSLoyaltyCard tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSLoyaltyCard createItemFromJSONObject(JSONObject jsonLineItem){
		POSLoyaltyCard loyaltyCard = new POSLoyaltyCard();
		try{
			loyaltyCard.setId(jsonLineItem.getInt("loyaltyID"));
			loyaltyCard.setBirthDay(timeFormat.parse(jsonLineItem.getString("birthday")));
			if(!jsonLineItem.isNull("cardNumber")){
				loyaltyCard.setCardNumber(jsonLineItem.getLong("cardNumber"));
			} else {
				loyaltyCard.setCardNumber(null);
			}
			loyaltyCard.setCurrentBalance(jsonLineItem.getDouble("currentBalance"));
			loyaltyCard.setEarnedPoints(jsonLineItem.getInt("earnedPoints"));
			loyaltyCard.setEmail(jsonLineItem.getString("email"));
			loyaltyCard.setFirstName(jsonLineItem.getString("firstName"));
			loyaltyCard.setGrowlerCount(jsonLineItem.getInt("growlerCount"));
			loyaltyCard.setLastName(jsonLineItem.getString("lastName"));
			loyaltyCard.setPhoneNumber(jsonLineItem.getLong("phoneNumber"));
			loyaltyCard.setEarnedGrowlers(jsonLineItem.getInt("earnedGrowlers"));
			return loyaltyCard;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSLoyaltyCard item){
		try{
			JSONObject json = new JSONObject();
			json.put("loyaltyID", item.getId());
			json.put("birthday", getDate(item.getBirthDay()));
			json.put("currentBalance", item.getCurrentBalance());
			json.put("earnedPoints", item.getEarnedPoints());
			json.put("email", item.getEmail());
			json.put("firstName", item.getFirstName());
			json.put("growlerCount", item.getGrowlerCount());
			json.put("lastName", item.getLastName());
			json.put("phoneNumber", item.getPhoneNumber());
			json.put("earnedGrowlers", item.getEarnedGrowlers());
			if(item.getCardNumber()!=null){
				json.put("cardNumber", item.getCardNumber());
			} else {
				json.put("cardNumber", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSLoyaltyCard getEmptyEntity(){
		POSLoyaltyCard entity = new POSLoyaltyCard();
		return entity;
	}

}
